package 多线程.多线程创建.线程安全.同步代码块;

import java.util.ArrayList;
import java.util.List;

public class MyTicket implements Runnable {
    private int ticket = 3;

    private List<String> object = new ArrayList<>(100);

    @Override
    public void run() {

        while (true) {

            synchronized (object) {
                if (ticket < 0 || ticket == 0) {
                    return;
                }
                System.out.println(Thread.currentThread().getName() + "抢到了第" + ticket + "张票");
                --ticket;
            }
        }
    }
}
